import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraJobApi } from '#/api/infra/job';
import type { DescriptionItemSchema } from '#/components/description';

import { h, markRaw } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDateTime } from '@vben/utils';

import { Timeline } from 'ant-design-vue';

import { CronTab } from '#/components/cron-tab';
import { DictTag } from '#/components/dict-tag';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.infra.job.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.job.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'handlerName',
      label: $t('views.infra.job.handlerName'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.job.handlerNamePlaceholder'),
      },
      dependencies: {
        triggerFields: ['id'],
        disabled: (values) => !!values.id,
      },
      rules: 'required',
    },
    {
      fieldName: 'handlerParam',
      label: $t('views.infra.job.handlerParam'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.job.handlerParamPlaceholder'),
      },
    },
    {
      fieldName: 'cronExpression',
      label: $t('views.infra.job.cronExpression'),
      component: markRaw(CronTab),
      componentProps: {
        placeholder: $t('views.infra.job.cronExpressionPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'retryCount',
      label: $t('views.infra.job.retryCount'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.infra.job.retryCountPlaceholder'),
        min: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'retryInterval',
      label: $t('views.infra.job.retryInterval'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.infra.job.retryIntervalPlaceholder'),
        min: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'monitorTimeout',
      label: $t('views.infra.job.monitorTimeout'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.infra.job.monitorTimeoutPlaceholder'),
        min: 0,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.infra.job.name'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.job.namePlaceholder'),
      },
    },
    {
      fieldName: 'status',
      label: $t('views.infra.job.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_JOB_STATUS, 'number'),
        allowClear: true,
        placeholder: $t('views.infra.job.statusPlaceholder'),
      },
    },
    {
      fieldName: 'handlerName',
      label: $t('views.infra.job.handlerName'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.job.handlerNamePlaceholder'),
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 80,
    },
    {
      field: 'name',
      title: $t('views.infra.job.name'),
      minWidth: 120,
    },
    {
      field: 'status',
      title: $t('views.infra.job.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_JOB_STATUS },
      },
    },
    {
      field: 'handlerName',
      title: $t('views.infra.job.handlerName'),
      minWidth: 180,
    },
    {
      field: 'handlerParam',
      title: $t('views.infra.job.handlerParam'),
      minWidth: 140,
    },
    {
      field: 'cronExpression',
      title: $t('views.infra.job.cronExpression'),
      minWidth: 120,
    },
    {
      title: $t('common.action'),
      width: 240,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 详情页的字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'id',
      label: $t('views.common.id'),
    },
    {
      field: 'name',
      label: $t('views.infra.job.name'),
    },
    {
      field: 'status',
      label: $t('views.infra.job.status'),
      content: (data: InfraJobApi.Job) => {
        return h(DictTag, {
          type: DICT_TYPE.INFRA_JOB_STATUS,
          value: data?.status,
        });
      }
    },
    {
      field: 'handlerName',
      label: $t('views.infra.job.handlerName'),
    },
    {
      field: 'handlerParam',
      label: $t('views.infra.job.handlerParam'),
    },
    {
      field: 'cronExpression',
      label: $t('views.infra.job.cronExpression'),
    },
    {
      field: 'retryCount',
      label: $t('views.infra.job.retryCount'),
    },
    {
      label: $t('views.infra.job.retryInterval'),
      content: (data: InfraJobApi.Job) => {
        return data?.retryInterval ? 
        $t('views.infra.job.retryIntervalFormat1', [data.retryInterval]) : $t('views.infra.job.retryIntervalFormat2');
      },
    },
    {
      label: $t('views.infra.job.monitorTimeout'),
      content: (data: InfraJobApi.Job) => {
        return data?.monitorTimeout && data.monitorTimeout > 0
          ? $t('views.infra.job.monitorTimeoutFormat1', [data.monitorTimeout])
          : $t('views.infra.job.monitorTimeoutFormat2');
      },
    },
    {
      field: 'nextTimes',
      label: $t('views.infra.job.nextTimes'),
      content: (data: InfraJobApi.Job) => {
        if (!data?.nextTimes || data.nextTimes.length === 0) {
          return $t('views.infra.job.noNextTimes');
        }
        return h(Timeline, {}, () =>
          data.nextTimes?.map((time: Date) =>
            h(Timeline.Item, {}, () => formatDateTime(time)),
          ),
        );
      },
    },
  ];
}
